package com.example.elasticsearch.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * @Description
 * @Author Mokairui
 * @Since 2021/11/6
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class RestClientConfig extends AbstractElasticsearchConfiguration {
    private String host;
    private int port;

    // 集群配置
    private String host2;
    private int port2;

    private String host3;
    private int port3;

    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(host + ":" + port, host2 + ":" + port2, host3 + ":" + port3)
                .build();

        return RestClients.create(clientConfiguration).rest();

//        return new RestHighLevelClient(RestClient.builder(
//                new HttpHost(host, port, "http"),
//                new HttpHost(host2, port2, "http"),
//                new HttpHost(host3, port3, "http")
//        ));
    }
}
